﻿namespace MSHelpMe.ViewModels
{
    using System;
    using System.Windows;

    public class CapturingState : PhotoCaptureState
    {

        public CapturingState(PhotoCapture ui)
            : base(ui)
        {

        }

        public override void ShowHideControls()
        {
            ui.Dispatcher.BeginInvoke(
                () =>
                {
                    ui.txtError.Visibility = Visibility.Collapsed;

                    ui.viewfinder.Visibility = ui.focusBrackets.Visibility = Visibility.Visible;
                    ui.btnShutter.Visibility = Visibility.Visible;

                    ui.imgPreview.Visibility = ui.words.Visibility = Visibility.Collapsed;
                    ui.txtTip.Visibility = Visibility.Collapsed;
                    ui.scanProgress.Visibility = Visibility.Collapsed;
                    ui.scanProgress.IsIndeterminate = false;
                    ui.btnAccept.Visibility = ui.btnCapture.Visibility = Visibility.Collapsed;
                });
        }

        public override void FocusClick()
        {
            try
            {
                if (!this.ui.Cam.IsFocusSupported)
                {
                    return;
                }

                this.ui.Dispatcher.BeginInvoke(() => this.ui.focusBrackets.Visibility = Visibility.Visible);
                this.ui.Cam.Focus();
            }
            catch
            {
                this.ui.Dispatcher.BeginInvoke(() => this.ui.focusBrackets.Visibility = Visibility.Collapsed);
            }
        }

        public override void ShutterClick()
        {
            try
            {
                this.ui.Cam.CaptureImage();
            }
            catch (Exception ex)
            {
                this.ui.ChangeState(new ErrorState(this.ui));
                this.ui.Dispatcher.BeginInvoke(() => this.ui.txtError.Text = ex.Message);
            }
        }

        public override void RecaptureClick()
        {
            // Do nothing
        }

        public override bool AcceptClick()
        {
            return false;
        }
    }
}
